// pages/mgmyinfo/mgmyinfo.js
const app = getApp();
var funcs = require('../../static/js/funcs.js');
import {
  cities,
  areas
} from '../../static/js/city';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    modify_type: "姓名",
    display_flag:false,
    uname:"刘思良",
    phone:"18982556976",
    modify_flag: null,
    cities,
    areas,
    city: "",
    area: "",
    multiIndex: [0, 0],
    objectMultiArray: [cities, areas[0]]
  },

  modifyName(){
    console.log("修改名字")
    this.setData({
      modify_type:"名字修改",
      display_flag:true,
      modify_flag: 0,
      inputholder:"请输入名字",
      inputType:"text"
    })
  },
  modifyPhone(){
    console.log("修改手机号")
    this.setData({
      modify_type:"手机号换绑",
      display_flag:true,
      modify_flag: 1,
      inputholder:"请输入手机号",
      inputType:"number"
    })
  },
  modifyOanum(){
    console.log("修改OA号")
    this.setData({
      modify_type:"员工OA号换绑",
      display_flag:true,
      modify_flag: 2,
      inputholder:"请输入员工OA号",
      inputType:"number"
    })
  },
  modifyIdcard(){
    console.log("修改身份证号")
    this.setData({
      modify_type:"身份证号换绑",
      display_flag:true,
      modify_flag: 3,
      inputholder:"请输入身份证号",
      inputType:"number"
    })
  },
  closeMask(){
    this.setData({
      display_flag: false,
      doType: 0
    })
  },
  inputValue(e){
    let onInput = e.detail.value
    console.log(e.detail.value)
    this.setData({
      value: onInput
    })
  },
  todoProcess(){
    let openid = app.globalData.openid
    let that = this

    if(this.data.modify_flag==0){
      console.log(this.data.value)
      if(!this.validateName(this.data.value)){
        console.log("格式错误")
        wx.showToast({
          title: '姓名格式错误',
          icon: 'error'
        })
        return;
      }
    }else if(this.data.modify_flag==1){
      if (this.vertifyPhone(this.data.value) == 0) {
        wx.showToast({
          title: '手机号格式错误',
          icon: 'error'
        })
        return;
      }
    }else if(this.data.modify_flag==2){
      if(!this.isNumeric(this.data.value+"")||(this.data.value+"").length!==6){
        wx.showToast({
          title: '格式错误',
          icon: 'error'
        })
        return;
      }
    }else if(this.data.modify_flag==3){
      if(!this.isValidID(this.data.value+"")){
        wx.showToast({
          title: '身份证格式错误',
          icon: 'error'
        })
        return;
      }
    }else if(this.data.modify_flag==4){
      if(this.data.city!=null&&this.data.city.length>0&&this.data.area!=null&&this.data.area.length>0){
        
      }else{
        console.log("城市信息错误")
        return;
      }
    }else if(this.data.modify_flag==5){
      if(this.data.value==null||this.data.value.length==0){
        wx.showToast({
          title: '格式错误',
          icon: 'error'
        })
        return;
      }
    }
    console.log(this.data.value)
    funcs.changePersonInfo(openid,openid,this.data.modify_flag,this.data.value).then(res=>{
      if(res.data.succeed){
        console.log(res.data.data)
        that.setData({
          uname:res.data.data.name,
          phone:res.data.data.telnum==null? "无":res.data.data.telnum,
          oanum:res.data.data.oanum,
          idcard:res.data.data.idcard==null? "无":res.data.data.idcard,
          inst:res.data.data.inst,
        })
        wx.showToast({
          title: '修改成功',
          icon: "success",
          duration: 2000
        })
      }else{
        wx.showToast({
          title: '修改成功',
          icon: "success",
          duration: 2000
        })
      }
      that.setData({
        display_flag:false,
        value:null
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let openid = app.globalData.openid
    let that = this
    funcs.getPersonInfo(openid).then(res=>{
      if(res.data.succeed){
        console.log(res.data.data)
        that.setData({
          uname:res.data.data.name,
          phone:res.data.data.telnum==null? "无":res.data.data.telnum,
          oanum:res.data.data.oanum,
          idcard:res.data.data.idcard==null? "无":res.data.data.idcard,
          inst:res.data.data.inst==null||res.data.data.inst.length==0? "无":res.data.data.inst, 
          city:res.data.data.city==null||res.data.data.city.length==0 ? "未填写":res.data.data.city,
          area:res.data.data.area==null||res.data.data.area.length==0 ? "未填写":res.data.data.area
        }) 
        if(that.data.city!="未填写"&&that.data.area!="未填写"){
          funcs.getAllByAreaName(that.data.city,that.data.area).then(res => {
            // console.log(res.data);
            let real_networkList = []
            let networkList = res.data.data;
            let mapidList = []
            let instList = []
            networkList.map((item, index) => {
              if (item.telephone != null && item.telephone.length > 0)
                real_networkList.push(item);
                instList.push(item.name)
                mapidList.push(item.map_id)
            })
            this.setData({
              networkList: real_networkList,
              inst_list: instList,
              mapid_list: mapidList
            })
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 手机号格式验证
  vertifyPhone: function (phone) {
    if (!/^1[3456789]\d{9}$/.test(phone)) {
      // 手机号格式不正确，提示用户
      wx.showToast({
        title: '手机格式错误',
        icon: 'error'
      });
      return 0;
    }
    return 1;
  },
  isValidID:function(id) {
    if (id.length !== 18) {
      return false;
    }
    const weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
    const checkCode = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
    let sum = 0;
    for (let i = 0; i < 17; i++) {
      sum += parseInt(id[i]) * weight[i];
    }
    const index = sum % 11;
    if (checkCode[index] !== id[17].toUpperCase()) {
      return false;
    }
    return true;
  },
  validateName:function (name) {
    // const regex = /^[a-zA-Z\u4e00-\u9fa5]+( [a-zA-Z\u4e00-\u9fa5]+)*$/;
    // return regex.test(name);
    // 收货人姓名校验(准则:姓名为2-8汉字)

      var regu = /^[\u4E00-\u9FA5]{2,8}$/;
      var re = new RegExp(regu);
      if (!re.test(name)) {
        return false;
      }
      return true;
  },
  isNumeric(str) {
    return !isNaN(Number(str));
  },
  bindColumnCityChange(event) {
    console.log('修改的列为', event.detail.column, '，值为', event.detail.value);
    switch (event.detail.column) {
      case 0: //修改第一列
        let arr = this.data.multiIndex;
        arr[0] = event.detail.value;
        arr[1] = 0;
        this.setData({
          objectMultiArray: [cities, areas[event.detail.value]],
          multiIndex: arr,
        })
        break;
      case 1: //修改第二列
        let arr2 = this.data.multiIndex;
        arr2[1] = event.detail.value;
        // console.log(arr2)
        this.setData({
          multiIndex: arr2,
        })
        break;
    }
    // console.log(this.data.multiIndex)
  },
  bindCityChange(event) {
    console.log('picker发送选择改变，携带值为', event.detail.value)
    let city_index = event.detail.value[0];
    let area_index = event.detail.value[1];
    let city = this.data.cities[city_index].name;
    let area = this.data.objectMultiArray[1][area_index].name;
    
    funcs.getAllByAreaName(city,area).then(res => {
      // console.log(res.data);
      let real_networkList = []
      let networkList = res.data.data;
      let mapidList = []
      let instList = []
      networkList.map((item, index) => {
        if (item.telephone != null && item.telephone.length > 0)
          real_networkList.push(item);
          instList.push(item.name)
          mapidList.push(item.map_id)
      })
      this.setData({
        networkList: real_networkList,
        inst_list: instList,
        mapid_list: mapidList
      })
    }).then(() => {
      this.setData({
        city: city,
        area: area,
        value:city + '-' + area,
        modify_flag:4
      })
      console.log(this.data.city)
      console.log(this.data.area)
      this.todoProcess()
    })
  },
  select_inst_func:function(e){
    console.log(this.data.networkList)
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let index = e.detail.value;
    this.setData({
      select_inst: this.data.inst_list[index],
      selectedmapid: this.data.mapid_list[index],
      modify_flag:5,
      value: this.data.mapid_list[index]
    })
    this.todoProcess()
  },
})